#include <iostream>

using namespace std;
istream& IOTest(istream&);

void IOState()
{
	IOTest(cin);

	double ival;
	istream::iostate oldState = cin.rdstate();
	cout << "Accept number only: " << endl;
	// read cin and test only for EOF; loop is executed even if there are other IO failures
	while(cin >> ival, !cin.eof())
	{
		if(cin.bad()) // input stream is corrupted; bail out
			throw runtime_error("IO stream corrupted");
		if(cin.fail())
		{
			cerr << "Bad data, try again \n";
			cin.clear(istream::failbit); // reset the stream
			// cin.clear(oldState); // reset cin to old state
			continue;
		}
		
		cout << ival << endl;		
	}
}

istream& IOTest(istream &io)
{
	double dval = 0;
	istream::iostate state = io.rdstate();
	while(io>>dval)
	{
		if(io.eof())
			break;

		cout << dval << endl;
	}

	io.setstate(state);
	return io;
}